<?php
class Admin_Page
{
  protected
    $config;
  
  public function register($cfg)
  {
    $this->config = $cfg;
    
    if (isset($cfg['parent']) && strlen($cfg['parent']))
    {
      add_submenu_page($parent,
        $cfg['page_title'],
        $cfg['menu_title'],
        $cfg['capability'],
        $cfg['handle'],
        array($this, 'display')
        );
    }
    else
    {
      add_menu_page(
        $cfg['page_title'],
        $cfg['menu_title'],
        $cfg['capability'],
        $cfg['handle'],
        array($this, 'display')
        );
    }
  }
  
  public function display()
  {
    $variables = array();
    if (isset($this->config['variables']))
      $variables = (array) $this->config['variables'];
    $variables['page_title'] = $this->config['page_title'];
    
    $form = Admin_Form::create($this->config['form']);
    $form->bind(get_option($form->getName()));
    
    if ($form->updated())
    {
      $form->save($_POST[$form->getName()]);
    }
    
    $template = Framework::Twig()->loadTemplate($this->config['template']);
    $template->display(array_merge(
      $variables,
      array('form' => $form)
      ));
  }
}